﻿/********************************************************************************
    Copyright (C) Binod Nepal, Planet Earth Solutions Pvt. Ltd., Kathmandu.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MixNP.Web.Classifieds.UserControls
{
    public partial class TextBoxControl : System.Web.UI.UserControl
    {

        public enum InputTypeEnum
        {
            Text, Number, Memo, Decimal, Date, NumberRange, DecimalRange, DateRange
        }

        public InputTypeEnum InputType { get; set; }

        /// <summary>
        /// Override to force simple IDs all around
        /// </summary>
        public override string UniqueID
        {
            get
            {
                return this.ID;
            }
        }
        /// <summary>
        /// Override to force simple IDs all around
        /// </summary>
        public override string ClientID
        {
            get
            {
                return this.ID;
            }
        }

        public string ControlLabel { get; set; }
        public string CssClass { get; set; }
        public int MaxLength { get; set; }
        //TODO
        public double MaxValue { get; set; }

        public Unit Height { get; set; }
        public string Text { get; set; }
        public string Text2 { get; set; }
        public string Note { get; set; }
        public bool? Enabled { get; set; }
        public bool Required { get; set; }
        public string RegularExpression { get; set; }
        public short TabIndex { get; set; }
        private bool isRangeControl { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.AddTextBox();
        }

        private void AddTextBox()
        {
            this.SetDisabled();
            this.SetRangeControl();

            if (this.TabIndex > 0)
            {
                TextBox1.TabIndex = this.TabIndex;
            }

            Literal1.Text = this.ControlLabel + ":";
            Literal1.ID = "Literal" + this.ID;
            Literal1.ClientIDMode = System.Web.UI.ClientIDMode.AutoID;

            if (!string.IsNullOrWhiteSpace(this.Note))
            {
                NoteLiteral.Text = this.Note;// +"<hr class=\"hr\" />";            
            }

            TextBox1.CssClass = this.CssClass;
            TextBox2.CssClass = this.CssClass;

            TextBox1.ID = this.ID;
            TextBox1.ClientIDMode = this.ClientIDMode;

            TextBox2.ID = this.ID + "Range";
            TextBox2.ClientIDMode = this.ClientIDMode;

            Label1.ID = this.ID + "Label";
            Label1.ClientIDMode = System.Web.UI.ClientIDMode.AutoID;

            NoteLiteral.ID = this.ID + "Note";
            NoteLiteral.ClientIDMode = System.Web.UI.ClientIDMode.AutoID;

            //Panel1.ID = this.ID + "Panel";
            //TextBoxPlaceHolder.ClientIDMode = this.ClientIDMode;

            Label1.Attributes.Add("for", TextBox1.ClientID);

            TextBox1.MaxLength = this.MaxLength;


            if (this.InputType == InputTypeEnum.Memo)
            {
                TextBox1.TextMode = TextBoxMode.MultiLine;
                TextBox1.Height = this.Height;
            }

            this.SetValidators();

            this.SetInitialValues();
        }

        private void SetInitialValues()
        {
            TextBox1.Text = this.Text;

            if (this.isRangeControl)
            {
                TextBox2.Text = this.Text2;
            }

        }

        private void SetRangeControl()
        {
            if (this.InputType == InputTypeEnum.DecimalRange || this.InputType == InputTypeEnum.DateRange || this.InputType == InputTypeEnum.NumberRange)
            {
                this.isRangeControl = true;
            }

            if (!this.isRangeControl)
            {
                TextBox2.NamingContainer.Controls.Remove(TextBox2);
            }
        }

        private void SetDisabled()
        {
            if (this.Enabled != null)
            {
                TextBox1.Enabled = (bool)this.Enabled;
            }

            if (this.isRangeControl)
            {
                TextBox2.Enabled = (bool)this.Enabled;
            }
        }

        private void SetValidators()
        {
            this.SetCompareValidators();
            this.SetRequiredFieldValidator();
            this.SetRegularExpressionValidator();
        }

        private void SetCompareValidators()
        {
            CompareValidator.ID = "Compare" + this.ID;
            CompareValidator.ClientIDMode = ClientIDMode.AutoID;

            CompareValidator.Enabled = true;

            if (this.isRangeControl)
            {
                CompareValidator.ControlToValidate = TextBox2.ID;
                CompareValidator.ControlToCompare = TextBox1.ID;

                if (this.InputType == InputTypeEnum.DecimalRange)
                {
                    CompareValidator.Type = ValidationDataType.Currency;

                    CompareValidatorRangeFrom.Type = ValidationDataType.Currency;
                    CompareValidatorRangeFrom.Enabled = true;
                    CompareValidatorRangeTo.Type = ValidationDataType.Currency;
                    CompareValidatorRangeTo.Enabled = true;
                }

                if (this.InputType == InputTypeEnum.DateRange)
                {
                    CompareValidator.Type = ValidationDataType.Date;

                    CompareValidatorRangeFrom.Type = ValidationDataType.Date;
                    CompareValidatorRangeFrom.Enabled = true;

                    CompareValidatorRangeTo.Type = ValidationDataType.Date;
                    CompareValidatorRangeTo.Enabled = true;
                }

                if (this.InputType == InputTypeEnum.NumberRange)
                {
                    CompareValidator.Type = ValidationDataType.Integer;

                    CompareValidatorRangeFrom.Type = ValidationDataType.Integer;
                    CompareValidatorRangeFrom.Enabled = true;
                    CompareValidatorRangeTo.Type = ValidationDataType.Integer;
                    CompareValidatorRangeTo.Enabled = true;
                }

                CompareValidatorRangeFrom.ID = "CompareRangeFrom" + this.ID;
                CompareValidatorRangeFrom.ClientIDMode = System.Web.UI.ClientIDMode.AutoID;
                CompareValidatorRangeFrom.ControlToValidate = TextBox1.ID;

                CompareValidatorRangeTo.ID = "CompareRangeTo" + this.ID;
                CompareValidatorRangeTo.ClientIDMode = System.Web.UI.ClientIDMode.AutoID;
                CompareValidatorRangeTo.ControlToValidate = TextBox2.ID;
            }
            else
            {
                CompareValidator.ControlToValidate = TextBox1.ID;
                CompareValidator.Operator = ValidationCompareOperator.DataTypeCheck;
                CompareValidatorRangeFrom.ControlToValidate = TextBox1.ID;
                CompareValidatorRangeTo.ControlToValidate = TextBox1.ID;

                if (this.InputType == InputTypeEnum.Decimal)
                {
                    CompareValidator.Type = ValidationDataType.Currency;
                    CompareValidator.ErrorMessage = "The value is not a valid decimal number.";
                }

                if (this.InputType == InputTypeEnum.Date)
                {
                    CompareValidator.Type = ValidationDataType.Date;
                    CompareValidator.ErrorMessage = "The value is not a valid date.";
                }


                if (this.InputType == InputTypeEnum.Number)
                {
                    CompareValidator.Type = ValidationDataType.Integer;
                    CompareValidator.ErrorMessage = "The value is not a number.";
                }

                CompareValidatorRangeTo.NamingContainer.Controls.Remove(CompareValidatorRangeTo);
                CompareValidatorRangeFrom.NamingContainer.Controls.Remove(CompareValidatorRangeFrom);
            }
        }

        private void SetRegularExpressionValidator()
        {
            RegularExpressionValidator1.ID = "RegularExpression" + this.ID;
            RegularExpressionValidator1.ClientIDMode = System.Web.UI.ClientIDMode.AutoID;
            RegularExpressionValidator1.ControlToValidate = TextBox1.ID;

            if (!string.IsNullOrWhiteSpace(RegularExpression))
            {
                RegularExpressionValidator1.ValidationExpression = this.RegularExpression;
                RegularExpressionValidator1.Enabled = true;
            }
        }

        private void SetRequiredFieldValidator()
        {
            RequiredFieldValidator1.ID = "Required" + this.ID;
            RequiredFieldValidator1.ClientIDMode = System.Web.UI.ClientIDMode.AutoID;
            RequiredFieldValidator1.Enabled = this.Required;
            RequiredFieldValidator1.ControlToValidate = TextBox1.ID;
        }

    }
}